package com.netease.xone.view;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.netease.xone.R;

public class MainTitlePopupWindow1 implements OnClickListener {
	private static final String TAG = MainTitlePopupWindow1.class.getSimpleName();
	
	public static final int MENU_ID_BASE = 0;
	public static final int MENU_TIME         = MENU_ID_BASE;
	public static final int MENU_TOPIC        = MENU_ID_BASE+1;
	public static final int MENU_MY_SUBSCRIBE = MENU_ID_BASE+2;
	public static final int MENU_MY_FOLLOW    = MENU_ID_BASE+3;
	public static final int MENU_MY_DISCUSS   = MENU_ID_BASE+4;
	private int mSelectedIndex = 0;
	private Context mContext;
	/**
	 * 主界面actionbar
	 */
	private ActionBar mActionBar;
	/**
	 * 资讯界面title点击弹出的PopupWindow
	 */
	/**
	 * 资讯类别：全部资讯、我订阅的、我关注的、我发布的
	 * 在选中全部资讯时候可以按时间排序和智能排序
	 */
	private List<String> mList;
	private OnClickListener mOnClickListener;
	private PopupWindow mPopupWindow = null;
	
	public MainTitlePopupWindow1(Context context, ActionBar actionbar, OnClickListener l, List<String> list, int index) {
		if (null == list || list.isEmpty() 
				|| list.size() < index-1
				|| index < 0) {
			throw new IllegalArgumentException(TAG);
		}
		mSelectedIndex = index;
		mContext = context;
		mActionBar = actionbar;
		mOnClickListener = l;
		mList = list;
		init();
	}
	
	private void init() {
		LinearLayout contentView = null;
		contentView = createContentView(mSelectedIndex);
		contentView.setBackgroundColor(mContext.getResources().getColor(R.color.gray));
		contentView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
		
		PopupWindow popupWindow = new PopupWindow(mContext);
		popupWindow.setTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setContentView(contentView);
		popupWindow.setWidth(contentView.getMeasuredWidth());
		popupWindow.setHeight(contentView.getMeasuredHeight());
		popupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				mPopupWindow = null;
			}
		});
    	
		popupWindow.showAsDropDown((View)mActionBar.getCustomView().getParent(), 0, 0);
		mPopupWindow = popupWindow;
	}
	
    private LinearLayout createContentView(int index) {
        LinearLayout layout = new LinearLayout(mContext);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(lp);
        layout.setOrientation(LinearLayout.VERTICAL);

        int i = 0;
        View view = null;
        TextView textView = null;
        ImageView select = null;
        View line = null;
        for (; i < mList.size(); i++) {
            view = View.inflate(mContext, R.layout.menu_fragmentinfo, null);
            textView = (TextView)view.findViewById(R.id.menu_text);
            select = (ImageView)view.findViewById(R.id.select);
            line = view.findViewById(R.id.menu_line);
            textView.setText(mList.get(i));
            textView.setId(MENU_ID_BASE + i);
            textView.setOnClickListener(this);
            if (index != i) {
                select.setVisibility(View.INVISIBLE);
            }

            layout.addView(view);
        }
        /**
         * 最后一个分隔线隐藏
         */
        line.setVisibility(View.GONE);
        
        return layout;
    }

	@Override
	public void onClick(View v) {
		if (null != mOnClickListener) {
		    mOnClickListener.onClick(v);
		}
		
		if (null != mPopupWindow && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
	}
	
	public void clear() {
		mContext = null;
		mActionBar = null;
		mOnClickListener = null;
		mList = null;
		if (null != mPopupWindow && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
		mPopupWindow = null;
	}
}
